home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / UTIL / MEMORY / VIRTUASRC / !Virtual / c / poll < prev   
Text File  |  1993-09-07  |  5KB  |  195 lines

  1. /*
  2.  * poll.c
  3.  * Part of the !Virtual distribution
  4.  * (c) bdb/nas/fo, 1992-3
  5.  */
  6.  
  7. #include "swis.h"
  8. #include "swiv.h"
  9. #include "wimp.h"
  10.  
  11. #include "virtual.h"
  12. #include "lib.h"
  13. #include "pager.h"
  14. #include "output.h"
  15. #include "asm.h"
  16. #include "poll.h"
  17.  
  18. void Poll(WKSP *w)
  19. { int reason;
  20.   int suspended=0;
  21.   int tm;
  22.   wimp_eventdata b;
  23.   char tbuf[256];
  24.  
  25.   if (!worksemaphore)
  26.     printf("Ooops!");
  27.   printflush(w);
  28.   usermode();
  29.   for (;;)
  30.   {
  31.     swi(OS_Release,IN(R0|R1|R2),3,&NormWriteC,w);
  32.  
  33.     if (w->nice_delay > 0)
  34.     {
  35.       swi(OS_ReadMonotonicTime,OUT(R0),&tm);
  36.       swi(Wimp_PollIdle,IN(R0|R1|R2)|OUT(R0),suspended,&b,tm+(w->nice_delay),&reason);
  37.     } else swi(Wimp_Poll,IN(R0|R1)|OUT(R0),suspended,&b,&reason);
  38.  
  39.     swi(OS_Claim,IN(R0|R1|R2),3,&NormWriteC,w);
  40.     switch (reason)
  41.     { case wimp_ENULL:
  42.         break;
  43.       case wimp_ESEND:
  44.       case wimp_ESENDWANTACK:
  45.         switch (b.msg.hdr.action)
  46.         {
  47.           case wimp_MCLOSEDOWN:
  48.             svcmode();
  49.             RealDoOff(w);
  50.             break;
  51.           case wimp_MSETSLOT:
  52.             /* if (b.msg.data.words[1]!=w->ourtask)
  53.               break; */
  54.             if (b.msg.data.words[0]>0)        /* min 2 pages */
  55.             { svcmode();
  56.               SetRealMem(w, b.msg.data.words[0]);
  57.               usermode();
  58.             }
  59.             b.msg.hdr.your_ref=b.msg.hdr.my_ref;
  60.             swi(Wimp_SendMessage,IN(R0|R1|R2),wimp_EACK,&b,b.msg.hdr.task);
  61.             break;
  62.           case /* TaskWindow_Input */  0x808c0:
  63.             { char *p=(char *)&b.msg.data.words[1];
  64.               int c;
  65.               for (c=b.msg.data.words[0];c>0;c--,p++)
  66.               { if (*p==w->EscapeChar && !w->EscapeCharEnable)
  67.                   w->EscapeCondition = 1;
  68.                 else
  69.                 { if (w->incount<INSIZE)
  70.                     w->inbuf[(w->instart+w->incount++)%INSIZE]=*p;
  71.                 }
  72.               }
  73.             }
  74.             break;
  75.  
  76.           case wimp_MDATASAVE:
  77.             /* This code (along with wimp_MRAMTRANSMIT) is for the reception of datasave messages
  78.              * from !edit,!srcedit,etc when they do 'Task input'. They should really use TaskWindow
  79.              * _Input messages, but don't, and they also ignore the size of buffer returned to them -
  80.              * always transferring upto 256 bytes in a chunk. Hence we receive to a temporary 256
  81.              * byte buffer (tbuf), and make a simple attempt to transfer this into the character input
  82.              * buffer (w->inbuf). If multiple 256-byte blocks are to be transferred, we ignore them.
  83.              * NAS - 07/09/93
  84.              */
  85. #ifdef DEBUG
  86.             printf("RamFetch:space %d\n", sizeof(tbuf));
  87.             printflush(w);
  88. #endif
  89.             b.msg.data.ramfetch.addr = (char *) &tbuf;
  90.             b.msg.data.ramfetch.nbytes = sizeof(tbuf);
  91.             b.msg.hdr.your_ref = b.msg.hdr.my_ref;
  92.             b.msg.hdr.action = wimp_MRAMFETCH;
  93.             b.msg.hdr.size = sizeof(b.msg.data.ramfetch)+sizeof(b.msg.hdr);
  94.             swi(Wimp_SendMessage,IN(R0|R1|R2),wimp_ESENDWANTACK,&b,b.msg.hdr.task);
  95. #ifdef DEBUG
  96.             printf("RamFetch: sent message\n");
  97.             printflush(w);
  98. #endif
  99.             break;
  100.  
  101.           case wimp_MRAMTRANSMIT:
  102.             { 
  103.               char *p=(char *)&tbuf;
  104.               int c;
  105. #ifdef DEBUG
  106.               printf("RamTransmit:%d\n",b.msg.data.ramtransmit.nbyteswritten);
  107.               printflush(w);
  108. #endif
  109.               for (c=b.msg.data.ramtransmit.nbyteswritten;c>0;c--,p++)
  110.               { if (*p==w->EscapeChar && !w->EscapeCharEnable)
  111.                   w->EscapeCondition = 1;
  112.                 else
  113.                 { if (w->incount<INSIZE)
  114.                     w->inbuf[(w->instart+w->incount++)%INSIZE]=*p;
  115.                 }
  116.               }
  117.             }
  118.             break;
  119.  
  120.           case /* TaskWindow_Suspend */ 0x808c6:
  121. #ifdef DEBUG
  122.             { int i;
  123.             printf("Suspended: PC=%x ",w->regs[15]);
  124.             for (i=0;i<=14;i++)
  125.               printf("R%d=%x ",i,w->regs[i]);
  126.             printf("\n");
  127.             printf("MemoryLimit=%x, ApplicationSpace=%x, CurrentlyActiveObject=%x\n",
  128.                         w->MemoryLimit, w->ApplicationSpace, w->CurrentlyActiveObject );
  129.             }
  130. #else
  131.             printf("Suspended\n");
  132. #endif
  133.             printflush(w);
  134.             suspended=1;
  135.             break;
  136.           case /* TaskWindow_Resume */ 0x808c7:
  137.             printf("Resumed\n");printflush(w);
  138.             suspended=0;
  139.             break;
  140.           case /* TaskWindow_Morite */ 0x808c4:
  141.             svcmode();
  142.             RealDoOff(w);
  143.             break;
  144.           default:
  145.             break;
  146.         }
  147.         continue;
  148.       default:
  149.         continue;
  150.     }
  151.     break;
  152.   }
  153.   svcmode();
  154.   FindPages(w);         /* in case its changed */
  155.   worksemaphore=1;
  156. }
  157.  
  158. int getc(WKSP *w)
  159. {
  160.   int c;
  161.   if (!w->incount && !w->EscapeCondition)
  162.   { Normal(w);
  163.     while (!w->incount && !w->EscapeCondition)
  164.       Poll(w);
  165.     Virtual(w);
  166.   }
  167.   if (w->EscapeCondition)
  168.     return -1;
  169.   c=w->inbuf[w->instart];
  170.   if (w->incount > 0) w->incount--;
  171.   if (++w->instart>=INSIZE)
  172.   {
  173.     w->instart=0;
  174.     w->incount=0;
  175.   }
  176.   return c;
  177. }
  178.  
  179. /* Instant return getc - lower level than getc() */
  180. int lowgetc(WKSP *w)
  181. {
  182.   int c;
  183.   if (!w->incount) return -1;
  184.   if (w->EscapeCondition)
  185.     return -1;
  186.   c=w->inbuf[w->instart];
  187.   if (w->incount > 0) w->incount--;
  188.   if (++w->instart>=INSIZE)
  189.   {
  190.     w->instart=0;
  191.     w->incount=0;
  192.   }
  193.   return c;
  194. }
  195.